import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

// Student class defining the data structure
class Student {
    private String id;
    private String name;
    private String major;
    private String admissionDate;

    // Constructor
    public Student(String id, String name, String major, String admissionDate) {
        this.id = id;
        this.name = name;
        this.major = major;
        this.admissionDate = admissionDate;
    }

    // Getters
    public String getId() { return id; }
    public String getName() { return name; }
    public String getMajor() { return major; }
    public String getAdmissionDate() { return admissionDate; }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", major='" + major + '\'' +
                ", admissionDate='" + admissionDate + '\'' +
                '}';
    }
}

public class StudentManagementSystem {
    public static void main(String[] args) {
        // Create Gson instance with pretty printing
        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        // Create at least 5 student objects
        List<Student> students = new ArrayList<>();
        students.add(new Student("S001", "张伟", "计算机科学", "2023-09-01"));
        students.add(new Student("S002", "王芳", "电子工程", "2023-09-01"));
        students.add(new Student("S003", "李明", "机械工程", "2024-09-01"));
        students.add(new Student("S004", "刘洋", "软件工程", "2024-09-01"));
        students.add(new Student("S005", "陈静", "数据科学", "2023-09-01"));

        // Serialize to JSON and save to file
        try (Writer writer = new FileWriter("students.json")) {
            gson.toJson(students, writer);
            System.out.println("学生数据已成功序列化并保存到 students.json");
        } catch (IOException e) {
            System.err.println("序列化错误: " + e.getMessage());
        }

        // Deserialize from file and print
        try (Reader reader = new FileReader("students.json")) {
            Type studentListType = new TypeToken<List<Student>>(){}.getType();
            List<Student> deserializedStudents = gson.fromJson(reader, studentListType);
            System.out.println("\n从文件中反序列化的学生数据:");
            for (Student student : deserializedStudents) {
                System.out.println(student);
            }
        } catch (IOException e) {
            System.err.println("反序列化错误: " + e.getMessage());
        }

    }
}